/** 
 * <p>Original Author:  jessefreeman</p>
 * <p>Class File: CamoDisplayStyle.as</p>
 * 
 * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:</p>
 * 
 * <p>The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.</p>
 * 
 * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.</p>
 * 
 * <p>Licensed under The MIT License</p>
 * <p>Redistributions of files must retain the above copyright notice.</p>
 *
 * <p>Revisions<br/> 
 * 	2.0  Initial version Jan 7, 2009</p>
 *
 */

package camo.core.style
{
	import camo.core.configs.AbstractConfig;
	import camo.core.utils.TypeHelperUtil;
	
	import flash.geom.Rectangle;
	
	public class CamoDisplayStyle extends AbstractConfig
	{
		
		public static const DELIMITER : String = " ";
		
		// Default repeat values supported in CSS
		public static const REPEAT:String = "repeat";
		public static const NO_REPEAT:String = "no-repeat";
		public static const REPEAT_X:String = "repeat-x";
		public static const REPEAT_Y:String = "repeat-y";
		
		public static const VISIBLE:String = "visible";
		public static const HIDDEN:String = "hidden";
		
		public static const SCROLL:String = "scroll";
		public static const AUTO:String = "auto";
		
		public static const TOP:String = "top";
		public static const MIDDLE:String = "middle";
		public static const BOTTOM:String = "bottom";
		
		public static const LEFT:String = "left";
		public static const CENTER:String = "center";
		public static const RIGHT:String = "right";
		
		public var x : Number = 0;
		public var y : Number = 0;
		public var alpha : Number = 1;
		public var rotation : Number = 0;
		public var cursor: String;
		public var visibility:String = VISIBLE;// visible hidden
		
		// Masking
		public var mask : Rectangle;
		public var overflow : String = VISIBLE; //visible hidden scroll auto
		
		// Others
		public var verticalAlign:String = TOP; //top middle bottom
		public var align:String = LEFT; //left center right
		public var zIndex:Number;
		
		//
		public var width : Number = 0;
		public var height : Number = 0;
		public var margin:OffsetStyle = new OffsetStyle();
		public var padding:OffsetStyle = new OffsetStyle();
		public var border:BorderStyle;
		public var background:BackgroundStyle;
		public var backgroundImage:String;
		
		/**
		 * 
		 * @param styleObject
		 * 
		 */		
		public function CamoDisplayStyle(styleObject:Object=null)
		{
			super(this);
			
			if(styleObject) parseStyleObject(styleObject);
		}
		
		/**
		 * <p>Goes through an object and validates and converts data.</p>
		 * 
		 * @param styleObject
		 * 
		 */		
		public function parseStyleObject(styleObject:Object):void
		{
			name = styleObject.styleName;
			
			// Width and Height are default properties and set to 0 if missing.
			width = (styleObject["width"]) ? Number(styleObject["width"]) : 0;
			height = (styleObject["height"]) ? Number(styleObject["height"]) : 0;
			
			// Margin and Padding are default and must be included.
			if (styleObject["margin"]) margin = TypeHelperUtil.stringToOffset(styleObject["margin"], DELIMITER);
			if (styleObject["padding"]) padding = TypeHelperUtil.stringToOffset(styleObject["padding"], DELIMITER);
			
			// Border is not a default value so if it exists we create a new Border instance.
			if (styleObject["border"]) border = TypeHelperUtil.stringToBorder(styleObject["border"], DELIMITER);
			
			// if border is present, check for the alpha color of that border.
			if(border)
				border.alpha = (styleObject["borderAlpha"]) ? Number(styleObject["borderAlpha"]) : 1;
			
			// Background color is not a default value
			if(styleObject["backgroundColor"])
			{
				background = new BackgroundStyle(TypeHelperUtil.stringToColor( styleObject["backgroundColor"] ) );
				if (styleObject["backgroundColorAlpha"]) background.alpha = Number( styleObject["backgroundColorAlpha"] );
			}
			
			if(styleObject["backgroundImage"])
			{
				/* First make sure there is an instance of the background before
				adding background image values. */
				if(!background) background= new BackgroundStyle();
				
				// Go through and apply any valid backgroundImage properties.
				backgroundImage = styleObject["backgroundImage"];
				if(styleObject["backgroundPosition"]) background.position = TypeHelperUtil.stringToPoint(styleObject["backgroundPosition"],DELIMITER);
				if(styleObject["backgroundScale9"]) background.scale9Grid = TypeHelperUtil.stringToRectangle(styleObject["backgroundScale9"], DELIMITER);
				if(styleObject["backgroundRepeat"]) background.repeat = validateRepeat(styleObject["backgroundRepeat"]); 	
			}
			
			if (styleObject.x) x = Number(styleObject.x);
			if (styleObject.y) y = Number(styleObject.y);
			if (styleObject.alpha) alpha = Number(styleObject.alpha);
			if (styleObject.rotation) rotation = Number(styleObject.rotation);
			if (styleObject.cursor) cursor = styleObject.cursor;
			if (styleObject.visibility) visibility = validateVisibility(styleObject.visibility);
			
			//if (styleObject.clip) mask = TypeHelperUtil.stringToRectangle( splitRect(styleObject.clip)[1], ",");
			
			if (styleObject.overflow) overflow = validateOverflow(styleObject.overflow);
			if (styleObject.verticalAlign) verticalAlign = validateVerticalAlign(styleObject.verticalAlign);
			if (styleObject.align) align = validateAlign(styleObject.align);
			if (styleObject.zIndex) zIndex = Number( styleObject.zIndex );
		}
		
		/**
		 * 
		 * @param value
		 * @return 
		 * 
		 */		
		internal function validateVisibility(value:String):String
		{
			switch(value)
			{
				case HIDDEN:
					return value;
				break;
				default:
					return VISIBLE;
				break;
			}
		}
		
		/**
		 * 
		 * @param value
		 * @return 
		 * 
		 */		
		internal function validateOverflow(value:String):String
		{
			switch(value)
			{
				case HIDDEN: case SCROLL: case AUTO:
					return value;
				break;
				default:
					return VISIBLE;
				break;
			}
		}
		
		/**
		 * 
		 * @param value
		 * @return 
		 * 
		 */		
		internal function validateRepeat(value:String):String
		{
			switch(value)
			{
				case REPEAT: case NO_REPEAT: case REPEAT_X: case REPEAT_Y:
					return value;
				break;
				default:
					return REPEAT;
				break;
			}
		}
		
		/**
		 * 
		 * @param value
		 * @return 
		 * 
		 */		
		internal function validateVerticalAlign(value:String):String
		{
			switch(value)
			{
				case TOP: case MIDDLE: case BOTTOM:
					return value;
				break;
				default:
					return null;
				break;
			}
		}
		
		/**
		 * 
		 * @param value
		 * @return 
		 * 
		 */		
		internal function validateAlign(value:String):String
		{
			switch(value)
			{
				case LEFT: case CENTER: case RIGHT:
					return value;
				break;
				default:
					return null;
				break;
			}
		}
		
		/**
		 * 
		 * @return new CamoDisplayConfig
		 * 
		 */		
		override public function clone():*
		{
			return new CamoDisplayStyle( super.clone() );
		}
		
	}
}